//
//  QFWSdkNetManager.m
//  AFNetworking
//
//  Created by ice on 2019/3/5.
//

#import "QFWSdkNetManager.h"

@implementation QFWSdkNetManager

+ (instancetype)shareManager
{
    static QFWSdkNetManager *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [QFWSdkNetManager manager];
        
        // 请求的数据类型
//        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
         manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        // 设置请求超时时间
        manager.requestSerializer.timeoutInterval = 10;
        
        // 请求头类型
        // [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        // [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        // 返回的数据类型
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
//        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript", @"application/x-javascript", @"text/plain", @"image/gif", nil];
        // manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        
        // AFSecurityPolicy
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        [manager.securityPolicy setAllowInvalidCertificates:NO];
    });
    
    [manager setupHTTPHeader];
    
    return manager;
}

- (void)setupHTTPHeader {
    
    //application/json
    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

}

#pragma mark - API

+ (void)GET:(NSString *)URLString
 parameters:(id)parameters
   response:(void (^)(NSURLSessionDataTask * task,id responseObject,NSError *error))response
{
    NSLog(@"|QWFSdk GET       |\n| URL       | %@\n| PARAM     | \n%@", URLString, [self dataToJsonString:parameters]);
    QFWSdkNetManager *manager = [QFWSdkNetManager shareManager];
    [manager GET:URLString
      parameters:parameters
        progress:nil
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             if (response) {
                 response(task, responseObject, nil);
             }
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             if (response) {
                 response(task, nil, error);
             }
         }
     ];
}

+ (void)POST:(NSString *)URLString
  parameters:(id)parameters
    response:(void (^)(NSURLSessionDataTask * task,id responseObject,NSError *error))response
{
    NSLog(@"|QWFSdk POST      |\n| URL       | %@\n| PARAM     | \n%@", URLString, [QFWSdkNetManager dataToJsonString:parameters]);
    QFWSdkNetManager *manager = [QFWSdkNetManager shareManager];
    [manager POST:URLString
       parameters:parameters
         progress:nil
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              if (response) {
                  response(task, responseObject, nil);
              }
          }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              if (response) {
                  response(task, nil, error);
              }
          }
     ];
}



+ (NSString *)dataToJsonString:(id)object
{
    if (!object) {
        return nil;
    }
    
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (!jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

@end
